import { getContent, postContent } from "@/service/api.js";
import { create } from "zustand";
const initialState = {
  title: "我是counter状态管理",
  counter: 0,
  list: [],
  name: "",
};
//set设置 state  get获取 state 或者获取action
export const useCounter = create((set, get) => ({
  ...initialState,
  add: () => set((state) => ({ counter: state.counter + 1 })),
  sub: (num) => set((state) => ({ counter: (state.counter -= num) })),
  setState: (payload) => {
    let obj = {};
    Object.keys(payload).forEach((key) => {
      obj[key] = payload[key];
    });
    set(obj);
  },
  reSetState: () => set({ ...initialState }),
  fetchGetContent: async (params) => {
    const res = await getContent(params);
    if (res && res?.code === 200) {
      set({ arrList: res.data.arrList });
    } else {
      set({ arrList: [] });
    }
  },
  fetchPostContent: async (params) => {
    const res = await postContent(params);
    if (res && res?.code === 200) {
      set({ name: res.data });
    } else {
      set({ name: "" });
    }
  },
  fetchOtherRequest: async (params) => {
    //如何调用其他异步实例
    await get().fetchPostContent(params);
  },
}));
